<template>
  <view class="container">
    <view class="header">
      <text class="title">添加学生</text>
    </view>

    <view class="form">
      <view class="form-item">
        <text class="label">姓名</text>
        <input class="input" v-model="form.name" placeholder="请输入姓名" />
      </view>
      
      <view class="form-item">
        <text class="label">年龄</text>
        <input class="input" type="number" v-model="form.age" placeholder="请输入年龄" />
      </view>
      
      <view class="form-item">
        <text class="label">性别</text>
        <radio-group @change="handleGenderChange">
          <label class="radio-label">
            <radio value="男" :checked="form.gender === '男'" />
            <text>男</text>
          </label>
          <label class="radio-label">
            <radio value="女" :checked="form.gender === '女'" />
            <text>女</text>
          </label>
        </radio-group>
      </view>
      
      <view class="form-item">
        <text class="label">专业</text>
        <input class="input" v-model="form.major" placeholder="请输入专业" />
      </view>
    </view>

    <button class="submit-btn" @click="submit">提交</button>
  </view>
</template>

<script>
import mockData from '@/static/mock-data.js';

export default {
  data() {
    return {
      form: {
        name: '',
        age: '',
        gender: '男',
        major: ''
      }
    };
  },
  methods: {
    handleGenderChange(e) {
      this.form.gender = e.detail.value;
    },
    submit() {
      // 简单验证
      if (!this.form.name) {
        uni.showToast({ title: '请输入姓名', icon: 'none' });
        return;
      }
      
      if (!this.form.age) {
        uni.showToast({ title: '请输入年龄', icon: 'none' });
        return;
      }
      
      if (!this.form.major) {
        uni.showToast({ title: '请输入专业', icon: 'none' });
        return;
      }
      
      // 添加学生
      mockData.addStudent({
        name: this.form.name,
        age: parseInt(this.form.age),
        gender: this.form.gender,
        major: this.form.major
      });
      
      uni.showToast({
        title: '添加成功',
        icon: 'success',
        duration: 1500,
        success: () => {
          setTimeout(() => {
            uni.navigateBack();
          }, 1500);
        }
      });
    }
  }
};
</script>

<style>
.container {
  padding: 30rpx;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.header {
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eee;
  margin-bottom: 40rpx;
}

.title {
  font-size: 40rpx;
  font-weight: bold;
  color: #333;
}

.form {
  background-color: white;
  border-radius: 16rpx;
  padding: 30rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.form-item {
  display: flex;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eee;
}

.label {
  width: 120rpx;
  font-size: 32rpx;
  color: #333;
}

.input {
  flex: 1;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  font-size: 32rpx;
}

.radio-group {
  display: flex;
}

.radio-label {
  display: flex;
  align-items: center;
  margin-right: 50rpx;
}

.submit-btn {
  margin-top: 60rpx;
  height: 90rpx;
  line-height: 90rpx;
  background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
  color: white;
  font-size: 36rpx;
  border-radius: 45rpx;
}
</style>